package com.toe.user.webapi.service.impl;

import com.toe.pojo.dto.user.UserRolePermissionAddDTO;
import com.toe.pojo.entity.user.UserRolePermission;
import com.toe.pojo.vo.user.UserRolePermissionVO;
import com.toe.user.webapi.mapper.UserRolePermissionMapper;
import com.toe.user.service.UserRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
public class UserRolePermissionServiceImpl extends ServiceImpl<UserRolePermissionMapper, UserRolePermission> implements UserRolePermissionService {

    @Autowired
    private UserRolePermissionMapper userRolePermissionMapper;

    @Override
    public void insert(UserRolePermissionAddDTO userRolePermissionAddDTO) {
        UserRolePermission userRolePermission = new UserRolePermission();
        BeanUtils.copyProperties(userRolePermissionAddDTO,userRolePermission);
        LocalDateTime date = LocalDateTime.now();
        userRolePermission.setCreateTime(date);
        userRolePermission.setUpdateTime(date);
        userRolePermissionMapper.insert(userRolePermission);
    }

    @Override
    public void delete(long id) {
        userRolePermissionMapper.deleteById(id);
    }

    @Override
    public void delete(long roleId, long PermissionId) {
        userRolePermissionMapper.deleteByRoleAndPermission(roleId, PermissionId);
    }

    @Override
    public Boolean selectOne(long roleId, long PermissionId) {
        UserRolePermissionVO userRolePermissionVO = userRolePermissionMapper.selectByRoleAndPermission(roleId, PermissionId);
        if (userRolePermissionVO == null)
            return false;
        return true;
    }
}
